package bidsanity



import grails.test.mixin.*
import org.junit.*

/**
 * Unit tests for the CustomerController class.
 */
@TestFor(CustomerController)
@Mock([Customer, Bid, Listing])
class CustomerControllerTests {

    void testCreate() {
        def customer = controller.create()
        Assert.assertNotNull(customer)
    }

    void testIndex(){
        Customer c = new Customer()
        c.id = 1
        c.save(validate: false)
        def controllerResults = controller.index()
        Assert.assertEquals(1, controllerResults.customerInstanceTotal)
    }


    void testShow() {
        Customer c = new Customer()
        c.id = 1
        c.save(validate: false)
        Assert.assertEquals(1, Customer.count)


        Map myParams = [id: c.id]
        controller.params.putAll(myParams)
        def customer = controller.show()
        Assert.assertNotNull(customer)
        
    }

    void testShowNoCustomer()
    {
        Map myParams = [id: 1]
        controller.params.putAll(myParams)
        def customer = controller.show()
        Assert.assertNull(customer)
        Assert.assertTrue(response.redirectedUrl == '/customer/index')

    }

    void testDeleteNoBids() {
        def cust = new Customer(emailAddress: 'seller@example.org', password: "testing").save(validate: true)
        Map myParams = [id:cust.id]
        controller.params.putAll(myParams)
        controller.delete()

        Assert.assertTrue(flash.message == 'default.deleted.message')
        Assert.assertTrue(response.redirectedUrl == '/customer/index')
    }

    void testDeleteWithListing() {
        def s = new Customer(emailAddress: 'seller@example.org', password: "testing").save(validate: true)
        def l = new Listing(
                name: 'TestListing',
                endDate: new Date() + 10,
                startingBidPrice: 1.0D,
                description: 'a test listing',
                seller: s).save(validate: true)
        Map myParams = [id:s.id]
        controller.params.putAll(myParams)
        controller.delete()

        Assert.assertTrue(flash.message == 'customer.has.listing')
        Assert.assertTrue(response.redirectedUrl == "/customer/show/${s.id}")
    }

    void testDeleteWithBid(){
        def s = new Customer(emailAddress: 'seller@example.org', password: "testing").save(validate: true)
        def bidder = new Customer(emailAddress: 'bidder@example.org', password: "testing").save(validate: true)
        def l = new Listing(
                name: 'TestListing',
                endDate: new Date() + 10,
                startingBidPrice: 1.0D,
                description: 'a test listing',
                seller: s).save(validate: true)
        new Bid(bidder: bidder, listing:  l, amount: 12.0D).save()

        Map myParams = [id:bidder.id]
        controller.params.putAll(myParams)
        controller.delete()

        Assert.assertTrue(flash.message == 'customer.bids.not.empty')
        Assert.assertTrue(response.redirectedUrl == "/customer/show/${bidder.id}")
    }
    
}
